package edu.gatech.cs2340.teamrocket.model.team;

import java.util.List;
import java.util.ArrayList;
import java.io.Serializable;
import edu.gatech.cs2340.teamrocket.model.job.Job;
import edu.gatech.cs2340.teamrocket.model.pokemon.Pokemon;
import edu.gatech.cs2340.teamrocket.model.storage.Backpack;

/**
 * This class represents the wagon of the group run by the player.
 * It also holds the speed at which the player travels and the amount of food
 * each traveler eats. Pokemon objects are also stored in here.
 *
 * @author Joe Tacheron
 *
 */
public class Team implements Serializable {

    /**
     * The number of party Pokemon.
     */
    public static final int NUM_TRAVELERS = 6;

    public static final int MIN_SPEED     = 1;
    public static final int SLOW_SPEED    = 2;
    public static final int MID_SPEED     = 3;
    public static final int FAST_SPEED    = 4;
    public static final int MAX_SPEED     = 5;
    public static final int DEFAULT_SPEED = MIN_SPEED;

    public static final int MIN_RATIONS = 1;
    public static final int MID_RATIONS = 2;
    public static final int MAX_RATIONS = 3;

    private int speed;
    private int rations;
    private Backpack inventory;

    private String name;
    private Job job;
    private List<Pokemon> team;

    public Team(){
        speed = DEFAULT_SPEED;
        rations = MID_RATIONS;
        team = new ArrayList<Pokemon>();
        inventory = new Backpack();

        for(int i = 0; i < NUM_TRAVELERS; i++)
            team.add(new Pokemon());
    }

    /*****************************************
     *                SETTERS                *
     *****************************************/

    public void namePokemon(int i, String name){
        team.get(i).setName(name);
    }

    /**
     * The setter for speed. It will set the speed within an acceptable range if
     * it is invalid.
     *
     * @param speed The new speed
     */
    public void setSpeed(int speed) {
        if(speed < MIN_SPEED){
            this.speed = MIN_SPEED;
        }
        else if(speed > MAX_SPEED){
            this.speed = MAX_SPEED;
        }
        else
            this.speed = speed;
    }

    /**
     * The setter for rations. It will set the rations within an acceptable
     * range if it is invalid.
     *
     * @param rations The new rations
     */
    public void setRations(int rations) {
        if(rations > MIN_RATIONS)
            this.rations = rations;
        else
            this.rations = MIN_RATIONS;
    }

    public void namePlayer(String n){
        name = n;
    }

    /**
     * Set the job and starting inventory money.
     *
     * @param j the new Job
     */
    public void setJob(Job j){
        job = j;
        inventory.setMoney(job.getStartingPokeDollars());
    }

    /*****************************************
     *                GETTERS                *
     *****************************************/

    public List<Pokemon> getPokemon(){
        return team;
    }

    /**
     * @return The current speed of the expedition
     */
    public int getSpeed() {
        return speed;
    }

    /**
     * @return The rations that each person will eat
     */
    public int getRations() {
        return rations;
    }

    public Backpack getInventory(){
        return inventory;
    }

    public String getName(){
        return name;
    }

    public Job getJob(){
        return job;
    }

    /**
     *
     * @return true if the entire team is fainted, false otherwise
     */
    public boolean teamIsFainted(){
        for(Pokemon t : team)
            if(!t.isFainted())
                return false;
        return true;
    }

    /**
     *
     * @return a summary in HTML format of this team
     */
    public String toHTML(){
        StringBuilder retval = new StringBuilder();

        retval.append("<html>");
        retval.append("<font size='4'>");
        retval.append("Health: ");
        retval.append("</font>");
        retval.append("<font size='3'>");

        for(Pokemon t : team){
            retval.append("<p>");
            retval.append(t.getName());
            retval.append(": ");
            if(t.isFainted()){
                retval.append("<span color='gray'>Fainted</span>");
            }
            else if(t.getHealth() <= Pokemon.MAX_HEALTH / 4) {
                retval.append("<span color='red'>");
                retval.append(t.getHealth());
                retval.append("</span>");
            }
            else if(t.getHealth() <= Pokemon.MAX_HEALTH / 2) {
                retval.append("<span color='#FFA500'>"); // orange
                retval.append(t.getHealth());
                retval.append("</span>");
            }
            else {
                retval.append(t.getHealth());
            }
            retval.append(" ");
            retval.append(t.getAilment().statusString());
            retval.append("</p>");
        }

        retval.append("</font>");

        retval.append("<font size='4'>");
        retval.append("<p></p>");
        retval.append("PP: ");
        retval.append("</font>");
        retval.append("<font size='3'>");

        for(Pokemon t : team){
            retval.append("<p>");
            retval.append(t.getName());
            retval.append(": ");
            retval.append(t.getPP());
            retval.append("</p>");
        }

        retval.append("</font>");
        retval.append("</html>");
        return retval.toString();
    }
}
